package org.data12.SpringbootDemo.controller;

import com.mysql.cj.util.DnsSrv;
import org.data12.SpringbootDemo.mapper.StudentMapper;
import org.data12.SpringbootDemo.model.Student;
import org.data12.SpringbootDemo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {
//    @Autowired
//    private StudentMapper studentMapper;
    @Autowired
    private StudentService studentService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/list")
    public String list(Model model) {
        List<Student> studentList = studentService.findAll();
        System.out.println(studentList);
        model.addAttribute("studentList",studentList);
        return "student/list";
    }

    @RequestMapping("/add")
    public String add(Model model){return "student/add";}

    @ResponseBody
    @PostMapping("/save")
    public String save(Student student){
        System.out.println(student);
        int res = studentService.save(student);
        if (res !=0 ) {
//            return "添加成功";
            return "redirect:/student/list";
        }else {
//            return "添加失败";
            return "redirect:/error";
        }
    }

    @RequestMapping("/delete")
    public String delete(Integer sno){
        System.out.println(sno);
        int res = studentService.delete(sno);
        if (res !=0 ) {
//            return "删除成功";
            return "redirect:/student/list";
        }else {
//            return "删除失败";
            return "redirect:/error";
        }
    }

    @RequestMapping("/edit")
    public String edit(Model model,Integer sno){
        Student student = studentService.findBySno(sno);
        System.out.println(student);
        model.addAttribute("student",student);
        return "student/edit";
    }

    @PostMapping("/update")
    public String update(Student student){
        int res = studentService.update(student);
        if (res != 0){
            return "redirect:/student/list";
        }else {
            return "redirect:/error";
        }
    }

    @RequestMapping("/error")
    public String error(){
        return "error";
    }

    @RequestMapping("/findByName")
    public String findByName(Model model,String sname){
        List<Student> students = studentService.findByName(sname);
        model.addAttribute("studentList",students);
        return "student/list";
    }

    @ResponseBody
    @RequestMapping("/setrediskey")
    public void setRedisKey(){
        ValueOperations operations = redisTemplate.opsForValue();
        operations.set("bookname","java");
    }

    @RequestMapping("/getrediskey")
    @ResponseBody
    public void getRedisKey(){
        ValueOperations operations = redisTemplate.opsForValue();
        Object object = operations.get("bookname");
        System.out.println(object);
    }

    @GetMapping("/ageDistribution")
    @ResponseBody
    public Map<String, Object> getAgeDistribution() {
        List<Student> students = studentService.findAll();
        Map<Integer, Integer> ageCount = new HashMap<>();
        for (Student student : students){
            int age = student.getAge();
            ageCount.put(age, ageCount.getOrDefault(age, 0) +1);
        }

        List<Integer> ages = new ArrayList<>(ageCount.keySet());
        List<Integer> counts = new ArrayList<>();
        for (int age : ages){
            counts.add(ageCount.get(age));
        }

        Map<String, Object> result = new HashMap<>();
        result.put("ages", ages);
        result.put("counts", counts);
        return result;
    }
    @RequestMapping("/chart")
    public String chart(){
        return "student/chart";
    }

}
